package com.frxuexi.user.view

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.User
import com.frxuexi.user.intent.UserIntent
import com.frxuexi.user.viewmodel.UserViewModel
import kotlinx.coroutines.launch

@Composable
fun RegisterPage(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        Log.i("===", "注册成功${it.data}")
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })
    var name by remember {
        mutableStateOf("")
    }
    var pass by remember {
        mutableStateOf("")
    }
    var pass2 by remember {
        mutableStateOf("")
    }

    var checked by remember {
        mutableStateOf(3)
    }

    Column {
        MyTitleBar(title = "注册", navHostController = navHostController)
        Column(
            verticalArrangement = Arrangement.spacedBy(20.dp),
            modifier = Modifier.padding(20.dp)
        ) {
            OutlinedTextField(modifier = Modifier.fillMaxWidth(),
                value = name,
                onValueChange = { name = it },
                label = { Text(text = "用户名") })
            OutlinedTextField(modifier = Modifier.fillMaxWidth(),
                value = pass,
                onValueChange = { pass = it },
                visualTransformation = PasswordVisualTransformation(),
                label = { Text(text = "密码") })
            OutlinedTextField(modifier = Modifier.fillMaxWidth(),
                value = pass2,
                onValueChange = { pass2 = it },
                visualTransformation = PasswordVisualTransformation(),
                label = { Text(text = "确认密码") })

            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "角色:")
                Spacer(modifier = Modifier.width(10.dp))
                Checkbox(checked = checked == 1, onCheckedChange = {
                    if (it) {
                        checked = 1
                    }
                })
                Text(text = "经理")
                Spacer(modifier = Modifier.width(10.dp))
                Checkbox(checked = checked == 2, onCheckedChange = {
                    if (it) {
                        checked =2
                    }
                })
                Text(text = "产品经理")
                Spacer(modifier = Modifier.width(10.dp))
                Checkbox(checked = checked == 3, onCheckedChange = {
                    if (it) {
                        checked = 3
                    }
                })
                Text(text = "员工")
            }
            Button(modifier = Modifier.fillMaxWidth(), onClick = {
                if (name.isNullOrEmpty() || pass.isNullOrEmpty() || pass2.isNullOrEmpty()) {
                    ToastUtils.showLong("输入内容不能为空")
                    return@Button
                }
                //1[0-9]{10}
                if (!name.matches(Regex("[0-9A-z_]{3,8}"))) {
                    ToastUtils.showLong("用户名不符合规则")
                    return@Button
                }
                if (pass != pass2) {
                    ToastUtils.showLong("两次密码不一致")
                    return@Button
                }

                vm.sendIntent(UserIntent.register(User(uname = name, upass = pass, role = checked)))
            }) {
                Text(text = "注册")
            }
        }
    }


}